import axios from "axios";
import React, { useState, useEffect } from "react";
import { Table, Button, notification } from "antd";
export default function Audit() {
  const { roleId, region, username } = JSON.parse(
    localStorage.getItem("token")
  );
  const [dataSource, setdataSource] = useState([]);
  const roleObj = {
    1: "superadmin",
    2: "admin",
    3: "editor",
  };
  useEffect(() => {
    axios.get("/news?auditState=1&_expand=category").then((res) => {
      const list = res.data;
      setdataSource(
        roleObj[roleId] === "superadmin"
          ? list
          : [
              ...list.filter((item) => item.username === username),
              ...list.filter(
                (item) =>
                  item.region === region && roleObj[item.roleId] === "editor"
              ),
            ]
      );
    });
  }, [roleId, region, username]);
  const columns = [
    {
      title: "新闻标题",
      dataIndex: "title",
      render(title, item) {
        return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>;
      },
    },
    {
      title: "作者",
      dataIndex: "author",
    },

    {
      title: "新闻分类",
      dataIndex: "category",
      render(category) {
        return <div>{category.title}</div>;
      },
    },

    {
      title: "操作",
      render(item) {
        return (
          <div>
            <Button
              type="primary"
              onClick={() => {
                handleAudit(item, 2, 1);
              }}
            >
              通过
            </Button>
            <Button
              type="primary"
              danger
              onClick={() => {
                handleAudit(item, 3, 0);
              }}
            >
              驳回
            </Button>
          </div>
        );
      },
    },
  ];
  const handleAudit = (item, auditState, publishState) => {
    setdataSource(dataSource.filter((data) => data.id !== item.id));

    axios
      .patch(`/news/${item.id}`, {
        auditState,
        publishState,
      })
      .then((res) => {
        notification.open({
          message: "提示",
          description: `操作成功`,
        });
      });
  };
  return (
    <div>
      <Table
        dataSource={dataSource}
        columns={columns}
        pagination={{ pageSize: 5 }}
        rowKey={(item) => item.id}
      />
    </div>
  );
}
